<!-- 配置车辆弹窗 -->
<template>
  <div>
    <el-dialog
      :title="titleInfo.text"
      :visible.sync="dialogFormVisible"
      width="640px"
      close="dialogFormH"
    >
      <div class="header">
        <div class="num">
          <label>司机编号：</label>
          <span>{{ driverInfo.userId || '' }}</span>
        </div>
        <div class="name">
          <label>司机名称：</label>
          <span>{{ driverInfo.name || '' }}</span>
        </div>
        <div class="iphone">
          <label>司机电话：</label>
          <span>{{ driverInfo.mobile || '' }}</span>
        </div>
        <div class="agency">
          <label>所属机构：</label>
          <span>{{ driverInfo.agency?.name || '' }}</span>
        </div>
      </div>
      <div class="serchBox">
        <label style="color: #606266;">配置车辆：</label>
        <el-select
          v-model="selected"
          placeholder="请输入车牌号"
          filterable
          @change="handleChange"
        >
          <el-option
            v-for="item in vehiclesInfo"
            :key="item.id"
            :label="item.licensePlate"
            :value="item.licensePlate"
          >
          </el-option>
        </el-select>
      </div>
      <el-table
        :key="tableKey"
        ref="multipleTable"
        :data="dataList"
        style="width: 584px; margin: 0px auto;"
        stripe
        :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
      >
        <el-table-column
          align="left"
          label="车牌号"
          width="211"
          prop="licensePlate"
        >
        </el-table-column>
        <el-table-column
          align="left"
          label="车辆状态"
          width="211"
          prop="workStatus"
        >
          <template v-slot="{ row }">
            <div
              class="tableColumn-status"
              :class="{ 'stop-use': !row.workStatus }"
            >
              {{ row.workStatus ? '可用' : '停用' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template v-slot="{ row }">
            <el-link
              type="primary"
              :underline="false"
              @click="handleVehicleDetail(row.id)"
            >查看</el-link>
            <el-divider direction="vertical" />
            <el-link
              type="danger"
              :underline="false"
              @click="deleteVehicles(row.id ,row.licensePlate)"
            >删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center; padding-bottom: 20px; width: 600px;"
      >
        <el-button
          type="warning"
          style="width: 90px"
          class="save-btn"
          @click="confirm"
        >确认</el-button>
        <el-button
          plain
          style="width: 90px; color: #2a2929; margin-left: 10px"
          class="cancel-btn"
          @click="dialogFormH"
        >取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { driverArrangeCar } from '@/api/transit'
export default {
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    vehiclesInfo: {
      type: Array,
      required: true
    },
    driverInfo: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      tableKey: 0,
      dialogFormVisible: false,
      form: {},
      dataList: [],
      selected: ''
    }
  },
  methods: {
    deleteVehicles (id, licensePlate) {
      this.dataList = this.dataList.filter((item) => item.id !== id)
      this.selected = ''
    },
    async confirm () {
      const res = await driverArrangeCar({
        driverIds: [this.driverInfo.userId],
        truckId: this.dataList[0].id
      })
      if (res.code === 200) {
        this.$message.success('安排车辆成功')
        this.$emit('newDataes')
        this.dialogFormVisible = false
        this.dataList = []
        this.selecteds = []
      } else {
        this.$message.error(res.msg)
      }
    },
    handleVehicleDetail (id) {
      this.$router.push({
        path: '/transit/vehicle-detail',
        query: { id, formUrlName: '车辆管理' }
      })
    },
    handleChange (value) {
      this.selected = value
      this.dataList = this.vehiclesInfo.filter(item => item.licensePlate === value)
    },
    dialogFormV () {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.selected = this.$props.driverInfo.truck.licensePlate
        this.dataList = [this.$props.driverInfo.truck]
      })
    },
    dialogFormH () {
      this.dialogFormVisible = false
      this.dataList = []
    }
  }
}
</script>

<style lang="scss" scoped>
.serchBox {
  width: 584px;
  margin: 0 auto;
  margin-top: 19px;
  .el-select {
    width: 238px;
  }
  margin-bottom: 20px;
}
.workTime {
  white-space: nowrap;
}
.header {
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 584px;
  height: 90px;
  background-color: #fbfafa;
  flex-wrap: wrap;
  padding: 21px 52px;
  // 使得整体内容距离上部分的距离为20px
  margin: 0 8px;
  margin-top: -10px;
  margin-bottom: 0;
  .el-input {
    width: 240px;
  }
  div {
    flex: 40%;
     // 使包裹的所有div的上下间距为14px
    // margin-bottom: 14px;
  }
  .num,
  .iphone{
    flex: 60%;
  }
  .num,.name{
    margin-bottom: 14px;
  }
  label {
    width: 71px;
    height: 20px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    font-size: 14px;
    color: #20232A;
    letter-spacing: 0.16px;
    text-align: right;
  }
  span {
    color: #818693;
    font-size: 14px;
  }
}
.pagination {
  margin: 32px 0 44px !important;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px !important;
  color: #20232a !important;
}
.selected-driver {
  margin-top: 18px;
}
</style>
